package com.qinglei.recoup.longconn.service;

import ch.qos.logback.core.joran.conditional.ElseAction;
import com.google.common.collect.EvictingQueue;

import java.util.*;

/**
 * 呼吸心率BPM数据再处理
 * @Date 2021-08-10
 *
 */
public class BreathHeartBpmReprocess {

    private Queue<Double>  buffer_HeartReference_P2NL20= EvictingQueue.create(20);
    private Queue<Double>  buffer_HeartReference_P2NL30= EvictingQueue.create(30);
    private Queue<Double>  buffer_HeartReference_P2NL60= EvictingQueue.create(60);

    private  Integer  HeartReference1 = 0;
    private  Integer  HeartReference2 = 0;
    private  Integer  HeartReference3 = 0;
    private  Integer  HeartReference4 = 0;


    {
        for (int i=0;i<60;i++){
            buffer_HeartReference_P2NL20.offer(-10.0);
            buffer_HeartReference_P2NL30.offer(-10.0);
            buffer_HeartReference_P2NL60.offer(-10.0);
        }
    }

    /**
     *
     * @param BreathBPMReference   呼吸参考值
     * @param OutBedFlag  离床标志
     * @param HeartBPM   心跳BPM 会更新
     * @param BreathBPM  呼吸BPM  会更新
     * @param HeartP2NL   心跳峰值旁瓣比
     * @return
     */
    public Map<String,Object> fun_bpm_reprocess(Integer BreathBPMReference, Integer OutBedFlag,
                                                      Integer HeartBPM,Integer  BreathBPM, Double HeartP2NL){


        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("BreathBPMReference",BreathBPMReference);
        resultMap.put("OutBedFlag",OutBedFlag);
        resultMap.put("HeartP2NL",HeartP2NL);
        buffer_HeartReference_P2NL20.offer(HeartP2NL);
        buffer_HeartReference_P2NL30.offer(HeartP2NL);
        buffer_HeartReference_P2NL60.offer(HeartP2NL);


        if(countNum(buffer_HeartReference_P2NL60,-4.0)>=  buffer_HeartReference_P2NL60.size() - 2) {
            HeartReference1 = HeartBPM; //100%可信度
        }
        if(countNum(buffer_HeartReference_P2NL30,-4.0)>=  buffer_HeartReference_P2NL30.size() - 2 ){
            HeartReference2 = HeartBPM; //80%可信度
        }
        if(countNum(buffer_HeartReference_P2NL30,-6.0)>=  buffer_HeartReference_P2NL30.size() - 2){
            HeartReference3 = HeartBPM; //60%可信度
        }
        if(countNum(buffer_HeartReference_P2NL20,-6.0)>=  buffer_HeartReference_P2NL20.size() - 2){
            HeartReference4 = HeartBPM; //40%可信度
        }

        // 可靠值融合
        // 有可靠值
        int HeartBPMReference;
        int HeartBPMReference_LocalReserve;
        int BreathBPMReference_LocalReserve;
        if (HeartReference1 != 0.0) {
            /*  在有可靠值1的情况下 */
            if (fabs(HeartReference4 - HeartReference1) >= 10.0) {
                /*  可靠值4不可靠 */
                if (fabs(HeartReference3 - HeartReference1) >= 10.0) {
                    /*  可靠值3不可靠 */
                    if (fabs(HeartReference2 - HeartReference1) >= 10.0) {
                        /*  可靠值2不可靠 */
                        /*  只有可靠值1可靠 */
                        HeartBPMReference = HeartReference1;
                    } else {
                        /*  可靠值2可靠 */
                        HeartBPMReference =(int)( (HeartReference2 + HeartReference1) / 2.0);
                    }
                } else {
                    /*  可靠值3可靠 */
                    HeartBPMReference = (int)(((HeartReference3 + HeartReference2) +
                            HeartReference1) / 3.0);
                }
            } else {
                /*  可靠值4可靠 */
                HeartBPMReference = (int)((((HeartReference4 + HeartReference3) +
                        HeartReference2) + HeartReference1) / 4.0);
            }
        } else if (HeartReference2 != 0.0) {
            /*  在无可靠值1，但有可靠值2的情况下 */
            if (fabs(HeartReference4 - HeartReference2) >= 10.0) {
                /*  可靠值4不可靠 */
                if (fabs(HeartReference3 - HeartReference2) >= 10.0) {
                    /*  可靠值3不可靠 */
                    HeartBPMReference = HeartReference2;
                } else {
                    /*  可靠值3可靠 */
                    HeartBPMReference = (int)((HeartReference3 + HeartReference2) / 2.0);
                }
            } else {
                /*  可靠值4可靠 */
                HeartBPMReference = (int)(((HeartReference4 + HeartReference3) +
                        HeartReference2) / 3.0);
            }
        } else if (HeartReference3 != 0.0) {
            /*  在无可靠值1\2，但有可靠值3的情况下 */
            if (fabs(HeartReference4 - HeartReference3) >= 10.0) {
                /*  可靠值4不可靠 */
                HeartBPMReference = HeartReference3;
            } else {
                /*  可靠值4可靠 */
                HeartBPMReference = (int)((HeartReference4 + HeartReference3) / 2.0);
            }
        } else if (HeartReference4 != 0.0) {
            /*  在无可靠值1\2\3，但有可靠值4的情况下 */
            HeartBPMReference = HeartReference4;
        } else {
            /*  可靠值1\2\3\4都为0 */
            HeartBPMReference =(int) 70.0;
        }


        HeartBPMReference_LocalReserve = HeartBPMReference;


        if (BreathBPMReference != 0.0) {
            BreathBPMReference_LocalReserve = BreathBPMReference;

        }else {
            BreathBPMReference_LocalReserve = 15;
        }

        if (OutBedFlag == 1.0) {
            HeartBPM = HeartBPMReference_LocalReserve;
            BreathBPM = BreathBPMReference_LocalReserve;
            resultMap.put("HeartBPM",HeartBPM);
            resultMap.put("BreathBPM",BreathBPM);
        } else {
            if (HeartBPM != 0)
            {
                HeartBPM = (int)Math.round((0.3 * (HeartBPM -
                        HeartBPMReference_LocalReserve) + HeartBPMReference_LocalReserve));
            }
            else
            {
                HeartBPM = 0;
            }
            if (BreathBPM != 0)
            {
                BreathBPM = (int)Math.round((0.2 * (BreathBPM -
                        BreathBPMReference_LocalReserve) + BreathBPMReference_LocalReserve));
            }
            else {
                BreathBPM = 0;
            }

            resultMap.put("HeartBPM",HeartBPM);
            resultMap.put("BreathBPM",BreathBPM);
            //System.out.println("HeartBPM-------------------------:   "+HeartBPM);
        }




        return resultMap;
    }


    public static  int countNum(Queue<Double> queue, Double num){
        int count = 0;
        Queue<Double> queueTmp = new ArrayDeque<>();
        queueTmp.addAll(queue);
        while (!queueTmp.isEmpty()){
            if(queueTmp.poll()>num){
                count++;
            }
        }
        return  count;
    }

    public static  Integer fabs(Integer d){
        return  Math.abs(d);
    }

}
